package com.wwc.bwcj.activity

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.wwc.bwcj.fragment.CartFragment
import com.wwc.bwcj.fragment.OrdersFragment
import com.wwc.bwcj.fragment.OrderFragment
import com.wwc.bwcj.R

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        //底部导航视图组件引用
        val bottomNav = findViewById<BottomNavigationView>(R.id.bottom_nav)

        // 首次创建，默认初始显示「点单」页面
        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                .replace(R.id.fragment_container, OrderFragment())
                .commit()
        }

        // 底部导航项点击监听
        bottomNav.setOnItemSelectedListener { item ->
            when (item.itemId) {
                //点单
                R.id.menu_order -> {
                    supportFragmentManager.beginTransaction()
                        .replace(R.id.fragment_container, OrderFragment())
                        .commit()
                    true
                }
                //购物车
                R.id.menu_cart -> {
                    supportFragmentManager.beginTransaction()
                        .replace(R.id.fragment_container, CartFragment())
                        .commit()
                    true
                }
                //订单
                R.id.menu_orders -> {
                    supportFragmentManager.beginTransaction()
                        .replace(R.id.fragment_container, OrdersFragment())
                        .commit()
                    true
                }
                else -> false
            }
        }
    }
}